package com.leyou.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.impl.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /*
     *商品分页展示
     * */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> findSpuByPage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                            @RequestParam(name = "rows", defaultValue = "5") Integer rows,
                                                            @RequestParam(name = "key", required = false) String key,
                                                            @RequestParam(name = "saleable", required = false) Boolean saleable) {
        return ResponseEntity.ok(goodsService.findSpuByPage(page, rows, key, saleable));
    }
    /*
    * 商品上下架
    * */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam(name = "id") Long id, @RequestParam(name = "saleable") Boolean saleable) {
        goodsService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /*
    *商品修改回显(detail表)
    * */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> findDetailById(@RequestParam(name = "id") Long id) {
        return ResponseEntity.ok(goodsService.findSpuDetailById(id));
    }
    /*
     *商品修改回显(sku表)
     * */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>findSkuBySpuId(@RequestParam(name = "id") Long id) {
        return ResponseEntity.ok(goodsService.findSkuListBySpuId(id));
    }

    /*
    * 商品增加到数据库
    * */
    @PostMapping("/goods")
    public ResponseEntity<Void> goodsAdd(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 修改商品
     *
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 根据spu的id查询spu
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> findSpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.findSpuById(id));
    }

}
